package com.self.serviceedu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.self.commonutils.R;
import com.self.servicebase.handler.MyException;
import com.self.serviceedu.entity.EduTeacher;
import com.self.serviceedu.query.TeacherQuery;
import com.self.serviceedu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author xiaozeng
 * @since 2020-08-20
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
@Api(description = "讲师管理")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    //添加操作
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R save(
            @ApiParam(name = "eduTeacher",value = "讲师对象",required = true)
            @RequestBody EduTeacher eduTeacher){
        eduTeacherService.save(eduTeacher);
        return R.ok();
    }

    //根据Id查询
    @ApiOperation(value = "根据讲师ID查询")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id",value = "讲师ID",required = true)
            @PathVariable("id") String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("item",eduTeacher);
    }

    //根据讲师ID修改讲师
    @ApiOperation(value = "根据讲师Id修改讲师")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name = "id",value = "讲师ID",required = true)
            @PathVariable("id") String id,
            @ApiParam(name = "eduTeacher",value = "讲师对象",required = true)
            @RequestBody EduTeacher eduTeacher){

        eduTeacher.setId(id);
        eduTeacherService.updateById(eduTeacher);
        return R.ok();

    }

    //分页查询
    @PostMapping("{startPage}/{limit}")
    @ApiOperation(value = "分页查询讲师")
    public R pageList(
            @ApiParam(name = "startPage",value = "起始页",required = true)
            @PathVariable("startPage") Long startPage,

            @ApiParam(name = "limit",value = "每页显示记录数",required = true)
            @PathVariable("limit") Long limit,

            @ApiParam(name = "teacherQuery",value = "查询对象",required = true)
            @RequestBody(required = false) TeacherQuery teacherQuery){

        Page<EduTeacher> page = new Page<>(startPage, limit);

        System.out.println(teacherQuery);

        eduTeacherService.pageQuery(page, teacherQuery);
        List<EduTeacher> rows = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("total",total).data("rows",rows);

    }

    //查询所有
    @GetMapping
    @ApiOperation(value = "所有讲师列表")
    public R list(){

        /*
        try{
            int i = 10/0;

        }catch(Exception e){
            throw new MyException(20001,"出现自定义异常~");

        }*/

        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    //删除操作
    @DeleteMapping("{id}")
    @ApiOperation(value = "根据讲师ID删除讲师")
    public R removeById(
            @ApiParam(name = "id",value = "讲师ID",required = true)
            @PathVariable("id") String id){

        boolean result = eduTeacherService.removeById(id);

        if(result){
            return R.ok();
        }else{
            return R.error().message("删除失败");
        }

    }

}

